package com.andaily.domain.dto.log;

import com.andaily.domain.dto.AbstractDTO;
import com.andaily.domain.log.LogType;
import com.andaily.domain.shared.DateUtils;
import com.andaily.domain.shared.security.SecurityUtils;
import com.andaily.domain.user.User;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Shengzhao Li
 */
public class LogTypeDto extends AbstractDTO {

    private String name;
    private String description;
    private boolean defaultType;
    // Log amount of the type
    private int logAmount;

    private String creatorGuid;
    private String createDate;
    private boolean selectedType;

    public LogTypeDto() {
    }

    public LogTypeDto(LogType logType) {
        this.guid = logType.guid();
        this.name = logType.name();
        this.description = logType.description();
        this.defaultType = logType.defaultType();

        this.createDate = DateUtils.toDateText(logType.createTime());
        if (!this.defaultType) {
            final User creator = logType.creator();
            this.creatorGuid = creator.guid();
        }
        this.selectedType = logType.selectedType();
    }

    public boolean isAllowEdit() {
        return (!isNewly()
                && (!this.defaultType)
                && creatorGuid.equals(SecurityUtils.currentUserGuid()));
    }

    public boolean isSelectedType() {
        return selectedType;
    }

    public void setSelectedType(boolean selectedType) {
        this.selectedType = selectedType;
    }

    public String getCreateDate() {
        return createDate;
    }

    public String getCreatorGuid() {
        return creatorGuid;
    }

    public void setCreatorGuid(String creatorGuid) {
        this.creatorGuid = creatorGuid;
    }

    public int getLogAmount() {
        return logAmount;
    }

    public void setLogAmount(int logAmount) {
        this.logAmount = logAmount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isDefaultType() {
        return defaultType;
    }

    public void setDefaultType(boolean defaultType) {
        this.defaultType = defaultType;
    }

    public static List<LogTypeDto> toDtos(List<LogType> logTypes) {
        List<LogTypeDto> list = new ArrayList<LogTypeDto>(logTypes.size());
        for (LogType logType : logTypes) {
            list.add(new LogTypeDto(logType));
        }
        return list;
    }

    public LogType instance() {
        return new LogType(name, description);
    }
}
